package io.renren;

import com.alibaba.fastjson.JSON;

import java.util.*;

/**
 * Description
 *
 * @author ext.mahaifu1
 * @ClassName ListComparator
 * @date 2024-01-15
 */
public class ListComparator {

    public static void main(String[] args) {
        // 初始化objs1和objs2
        List<CompareConfDTO> objs1 = new ArrayList<>();
        CompareConfDTO obj1 = new CompareConfDTO("value1", "value1");
        CompareConfDTO obj2 = new CompareConfDTO("value2", "value2");
        CompareConfDTO obj3 = new CompareConfDTO("value3", "value3");
        CompareConfDTO obj4 = new CompareConfDTO("value_4", "value_4");
        objs1.add(obj1);
        objs1.add(obj2);
        objs1.add(obj3);
        objs1.add(obj4);

        List<CompareConfDTO> objs2 = new ArrayList<>();
        CompareConfDTO obj11 = new CompareConfDTO("value1", "value1");
        CompareConfDTO obj12 = new CompareConfDTO("value2", "value2");
        CompareConfDTO obj13 = new CompareConfDTO("value3", "value3");
        CompareConfDTO obj15 = new CompareConfDTO("value_5", "value_5");
        objs2.add(obj11);
        objs2.add(obj12);
        objs2.add(obj13);
        objs2.add(obj15);
        System.out.println("------1: " + JSON.toJSONString(objs1));
        System.out.println("------2: " + JSON.toJSONString(objs2));

        // 将列表转换为HashSet以便快速查找和删除操作
        Set<CompareConfDTO> set1 = new HashSet<>(objs1);
        Set<CompareConfDTO> set2 = new HashSet<>(objs2);


        // 计算objs2相对于objs1的新增对象
        Set<CompareConfDTO> newObjInObjs2 = new HashSet<>(set2);
        newObjInObjs2.removeAll(set1);

        // 计算objs1相对于objs2的新增对象
        Set<CompareConfDTO> newObjInObjs1 = new HashSet<>(set1);
        newObjInObjs1.removeAll(set2);

        //计算objs1和objs2的交集
        Set<CompareConfDTO> intersection = new HashSet<>(set1);
        intersection.retainAll(set2);


        System.out.println("objs1相对于objs2的新增对象: " + JSON.toJSONString(newObjInObjs1));
        System.out.println("objs2相对于objs1的新增对象: " + JSON.toJSONString(newObjInObjs2));
        System.out.println("objs1相对于objs1的交集对象: " + JSON.toJSONString(intersection));

        System.out.println(" ");

        for(CompareConfDTO incrs : newObjInObjs1){
            System.out.println("新增对象: " + JSON.toJSONString(incrs));
        }

        for(CompareConfDTO delObj : newObjInObjs2){
            System.out.println("删除对象: " + JSON.toJSONString(delObj));
        }

        for(CompareConfDTO inter : intersection){
            System.out.println("不变对象: " + JSON.toJSONString(inter));
        }

    }
}

